SimpleDateFormat = Packages.java.text.SimpleDateFormat;
ParseException   = Packages.java.text.ParseException;
Date             = Packages.java.util.Date;


function main( ui ) {
  
    var json = ui.getQueryParameters().getFirst("json");
    if ( json == null ){
        var ex = "{ exception: { message: 'no param provided'} }";
        return ex;
    }
    var o = new JSONObject( json );
    
    var TITLE_JSON_KEY="TITLE";
    var DESCRIPTION_JSON_KEY="DESCRIPTION";
    var STARTDATE_JSON_KEY="STARTDATE";
    var ENDDATE_JSON_KEY="ENDDATE";
    var EMAIL_JSON_KEY="EMAIL";
    var ID_CALENDAR_JSON_KEY="IDCALENDAR";
    var ADDRESS_JSON_KEY="ADDRESS";
    var IDEVENT_KEYEXT="id_Event_Entity";
    var COD_EVENT_JSASON_KEY="COD_EVENT";
    var LAT_JSON_KEY="LAT";
    var LNG_JSON_KEY="LNG";

    var service = new CalendarService("demo-calendar-1");
    
    
    var codEvent=o.getString(COD_EVENT_JSASON_KEY);
    var titleAppointment=o.getString(TITLE_JSON_KEY);
    var contentAppointment=o.getString(DESCRIPTION_JSON_KEY);
    var emailEmployee=o.getString(EMAIL_JSON_KEY);
    var startAppointment=o.getString(STARTDATE_JSON_KEY); //"2011-03-23T11:25:00";
    var endAppointment=o.getString(ENDDATE_JSON_KEY);
    var idCalendar= o.getString(ID_CALENDAR_JSON_KEY);
    var address = o.getString(ADDRESS_JSON_KEY);
    var lat = o.getDouble(LAT_JSON_KEY);
    var lng = o.getDouble(LNG_JSON_KEY);
 
    try {
           //ADD EVENT ENTITY
           var formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
           var dateFrom = formatter.parse(startAppointment);    
           var dateTo = formatter.parse(endAppointment);    
           var a = new EventEntity();
           a.from = dateFrom;
           a.to = dateTo;
           a.userId = emailEmployee;
           a.address = address;
           a.lat = lat;
           a.lng = lng;
           a.description=contentAppointment;
           a.contractId=codEvent;
           BOLEntityManager.createEvent( a );

    }catch( e ) {
         return e;
    }
  return "OK";
}
